package com.tfswx.jkgl.designer.bean;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import freemarker.template.Template;
import lombok.Data;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author ricky
 * @date 2022/1/16
 */


@Data
public class Clazz {

    private Template template;
    private String author = System.getProperty("user.name");
    private String date = DateUtil.format(new Date(), DatePattern.NORM_DATE_FORMAT);
    private String pkgName = "";
    private String className = "";
    private String desc = "";
    private List<String> anns = new ArrayList<>();
    private List<String> imports = new ArrayList<>();
    private List<Field> fields = new ArrayList<>();
    private List<Method> methods = new ArrayList<>();
    private List<Clazz> clazzes = new ArrayList<>();
    private List<String> impls = new ArrayList<>();

    public void addAnn(String s) {
        boolean add = anns.add(s);
    }

    public void addImort(String s) {
        boolean add = imports.add(s);
    }

    public void addField(Field field) {
        boolean add = fields.add(field);
    }

    public void addMethod(Method method) {
        methods.add(method);
    }

    public void addClazz(Clazz clazz) {
        clazzes.add(clazz);
    }

    public void addImpl(String impl) {
        impls.add(impl);
    }


    @Data
    public static class Field {
        private String lx;
        private String name;
        private List<String> anns = new ArrayList<>();

        public void addAnn(String s) {
            boolean add = anns.add(s);
        }
    }

    @Data
    public static class Method {
        private String returnType;
        private String paramType;
        private List<String> paramAnns = new ArrayList<>();
        private String name;
        private List<String> ywmsList;
        private List<String> anns = new ArrayList<>();

        public void addAnn(String s) {
            boolean add = anns.add(s);
        }

        public void addParamAnn(String s) {
            boolean add = paramAnns.add(s);
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }

            Method method = (Method) o;

            if (!returnType.equals(method.returnType)) {
                return false;
            }
            if (!paramType.equals(method.paramType)) {
                return false;
            }
            return name.equals(method.name);
        }

        @Override
        public int hashCode() {
            int result = returnType.hashCode();
            result = 31 * result + paramType.hashCode();
            result = 31 * result + name.hashCode();
            return result;
        }
    }

    @Override
    public String toString() {
        String s = "package " + pkgName + ";\n";
        for (String anImport : imports) {
            s += "\nimport " + anImport + ";";
        }
        s += "\n";
        for (String ann : anns) {
            s += "\n@" + ann;
        }
        s += "\npublic class " + className + "{";
        for (Field field : fields) {
            s += "\n";
            for (String ann : field.anns) {
                s += "\n\t@" + ann;
            }
            s += "\n\tprivate " + field.lx + " " + field.name + ";";
        }
        for (Method method : methods) {
            s += "\n";
            for (String ann : method.anns) {
                s += "\n\t@" + ann;
            }
            s += "\n\tpublic " + (method.returnType != null ? method.returnType : "void") + " " + method.name + "(){" +
                    "\nreturn;" +
                    "\n}";

        }
        s += "\n\n}";

        return s;
    }
}
